<?php

namespace App\System\Log\Repositories;

use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Log\Model\LogAdmin;
use App\System\Log\Model\LogSms;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class LogAdminRepository extends Repository
{
    use SinglePattern;

    /*
     * 记录列表
     * $getAll=false:获取分页数据 true：获取全部数据
     */
    public function getList($startTime='',$endTime='',$getAll=false)
    {
        if($getAll){
            $data = LogAdmin::with(['admin'])
                ->when($startTime,function ($query)use($startTime){
                    return $query->where('created_at','>=',$startTime);
                })
                ->when($endTime,function ($query)use($endTime){
                    return $query->where('created_at','<=',$endTime.' 23:59:59');
                })
                ->orderBy('created_at', 'desc')
                ->get();
        }else{
            $data = LogAdmin::with(['admin'])
                ->when($startTime,function ($query)use($startTime){
                    return $query->where('created_at','>=',$startTime);
                })
                ->when($endTime,function ($query)use($endTime){
                    return $query->where('created_at','<=',$endTime.' 23:59:59');
                })
                ->orderBy('created_at', 'desc')
                ->paginate();
        }
        return $data;
    }

    /**
     * 保存管理员操作日志
     * @param array $data $data['type'] $data['log_info']
     */
    public function saveAdminLog(array $data)
    {
        if(request()->is('admin/*') && $data){
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['admin_id'] = Session::get('adminid');
            $data['admin_ip'] = $_SERVER["REMOTE_ADDR"];
            $data['admin_url'] = $_SERVER["REQUEST_URI"];
            LogAdmin::insert($data);
        }
    }
}